﻿Imports System.IO
Imports System.Web
Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization
Imports MeoTom.Core.DAL

Namespace BLL
    Public Class LinkCat
        Public util As New Utilities.Common()
        Private lnk As New BLL.Link()

        Private _CatID As Integer
        Private _CatName As String
        Private _Description As String
        Private _TotalLink As Integer

        Public Property CatID() As Integer
            Get
                Return _CatID
            End Get
            Set(ByVal value As Integer)
                _CatID = value
            End Set
        End Property

        Public Property CatName() As String
            Get
                Return _CatName
            End Get
            Set(ByVal value As String)
                _CatName = value
            End Set
        End Property

        Public Property Description() As String
            Get
                Return _Description
            End Get
            Set(ByVal value As String)
                _Description = value
            End Set
        End Property

        Public Property TotalLink() As Integer
            Get
                Return _TotalLink
            End Get
            Set(ByVal value As Integer)
                _TotalLink = value
            End Set
        End Property

        Public lnkPrv As LinkCatProvider

        Public Sub New()
            lnkPrv = New LinkCatProvider()
        End Sub

        Public Sub Create()
            lnkPrv.Create(Me)
        End Sub

        Public Sub Update()
            lnkPrv.Update(Me)
        End Sub

        Public Sub GetByID()
            Try
                Dim reader As SqlClient.SqlDataReader = lnkPrv.GetByID(Me)
                If reader.Read() Then
                    Me.CatID = Integer.Parse(reader("CatID").ToString())
                    Me.CatName = reader("CatName").ToString()
                    Me.Description = reader("Description").ToString()
                End If
                reader.Close()
            Catch ex As Exception
            End Try
        End Sub

        Public Sub Delete()
            lnkPrv.Delete(Me)
        End Sub

        Public Function GetByName() As DataSet
            Return lnkPrv.GetByName(Me)
        End Function

        Public Function GetAll() As DataSet
            Return lnkPrv.GetAll()
        End Function

        Public Function WriteLinkCat(ByVal type As Integer, Optional ByVal SelectedID As String = "") As String
            Dim strOut As New StringBuilder()
            Dim dbTable As DataTable
            Try
                If type = 0 Or type = 3 Then
                    strOut.Append("<div class=""divCategory"">")
                ElseIf type = 4 Then
                    strOut.Append("<select name=""cboCategory"">")
                    strOut.Append("<option value=""0"">View all categories</option>")
                End If

                dbTable = Me.GetAll().Tables(0)
                For Each row As DataRow In dbTable.Rows
                    If type = 0 Or type = 3 Then
                        strOut.Append("<span class=""cboCatItem""><label><input type=""checkbox"" style=""width:1%;"" id=""cboCategory").Append(row("CatID").ToString()).Append(""" name=""cboCategory"" value=""").Append(row("CatID").ToString()).Append(""" />").Append(row("CatName").ToString).Append("</label></span>")
                    ElseIf type = 4 Then
                        strOut.Append("<option value=""").Append(row("CatID").ToString()).Append(""">").Append(row("CatName").ToString()).Append("</option>")
                    ElseIf type = 5 Then
                        If Integer.Parse(row("TotalLink").ToString()) > 0 Then
                            strOut.Append("<div class=""otherBox fl""><h3>").Append(row("CatName").ToString()).Append("</h3><div class=""hr fl"">&nbsp;</div><div class=""content-box""><ul>")
                            strOut.Append(lnk.GetByCat(0, Integer.Parse(row("CatID").ToString())))
                            strOut.Append("</ul></div></div>")
                        End If
                    End If
                Next

                If type = 0 Or type = 3 Then
                    strOut.Append("</div>")
                    If Not SelectedID = "" Then
                        strOut.Append("<script language=""javascript"" type=""text/javascript"">")
                        For Each strID As String In RegularExpressions.Regex.Split(SelectedID, ",")
                            If strID <> "" Then
                                strOut.Append("gmobj(""cboCategory").Append(strID).Append(""").checked = true;")
                            End If
                        Next
                        strOut.Append("</script>")
                    End If
                ElseIf type = 4 Then
                    strOut.Append("</select>")
                ElseIf type = 5 Then
                    Dim writer As New StreamWriter(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_Link.htm", False, System.Text.Encoding.UTF8)
                    writer.Write(strOut.ToString())
                    writer.Flush()
                    writer.Close()
                    strOut.Remove(0, strOut.Length)
                    strOut.Append("OK")
                End If
            Catch ex As Exception
            Finally
                dbTable = Nothing
            End Try
            Return strOut.ToString
        End Function
    End Class
End Namespace